import random

from com.xunai.saas.example.Common import CommonRequest


class AiInquiryApi(CommonRequest):
    """
    中医智能辩证系统
    """

    def getEparment(self):
        """
        获取科室信息
        :return:
        """
        rs = super().get("/xunai-saas/saasApi/v1/intelligentConsultation/getDepartmentList")
        print(rs.json())
        data = rs.json()['data']
        return data[random.randint(0, len(data) - 1)]['id']

    def addPatient(self):
        """
        新增患者信息
        :return:
        """
        param = {
            "patientAge": 30,
            "patientHeight": 170,
            "patientName": "测试1",
            "patientSex": 1,
            "patientWeight": 65,
            "type": 4
        }
        rs = super().post("/xunai-saas/saasApi/v1/intelligentConsultation/addPatient", param)
        print(rs.json())
        return rs.json()['data']['id']

    def start(self, epartmeneId, patientId):
        """
        开始
        :param epartmeneId:  科室ID
        :param patientId: 患者ID
        :return:
        """
        param = {"epartmeneId": epartmeneId, "patientId": patientId}
        rs = super().post("/xunai-saas/saasApi/v1/intelligentConsultation/start", param)
        print(rs.json())
        data = rs.json()['data']
        record = {"flowId": data['flowId'], "processId": data['processId'], "recordId": data['recordId'],
                  "answerList": [
                      data['answerList'][random.randint(0, len(data['answerList']) - 1)]['optionValue']
                  ]}
        self.next(record)

    def next(self, param):
        """
        循环回答问题
        :param param:
        :return:
        """
        rs = self.post("/xunai-saas/saasApi/v1/intelligentConsultation/next", param)
        print(rs.json())
        opt = rs.json()['data']
        if opt and opt['recordStatus'] != 2:
            print(rs.json()['data']['name'], rs.json())
            # 随机选择一个答案
            record = {"flowId": opt['flowId'], "processId": opt['processId'], "recordId": opt['recordId'],
                      "answerList": [self.getRandomAnswer(opt)]}
            self.next(record)
        else:
            self.result(opt['recordId'])

    def result(self, recordId):
        """
        结果
        :param recordId:
        :return:
        """
        rs = super().get("/xunai-saas/saasApi/v1/intelligentConsultation/detail?recordId=" + str(recordId))
        print("结果", rs.json())

    def getRandomAnswer(self, data):
        """
        模拟随机选取一个答案
        :param data:
        :return:
        """
        # 舌诊
        if data['optionType'] == 4:
            return "舌诊notifyCode"  # 舌诊notifyCode
        # 面诊
        if data['optionType'] == 5:
            return "面诊notifyCode"  # 面诊notifyCode
        if data['answerList'] and len(data['answerList']) > 0:
            return data['answerList'][random.randint(0, len(data['answerList']) - 1)]['optionValue']


if __name__ == "__main__":
    api = AiInquiryApi()
    api.start(api.getEparment(), api.addPatient())
